<?php

/** 
* Class containig utility methods for date and time
* @author Edio Hanoway Kepke <edio@aquiris.com>
* @version 1.0 
* @access public
*/ 
class DateTimeUtility
{
    
    /**
    * Returns the difference between a date and other
    * @access public
    * @return int
    * @param $dateOne datetime or date
    * @param $dateTwo datetime or date
    * @param $type char ['Y','M','D','H', 'I', 'S']
     *@param $tokenSlipt
    * @param $emailFacebook String Email using in the facebook
    */ 
    public static function differenceDate($type, $dateTimeOne, $dateTimeTwo = null)
    {                
        if(is_null($dateTimeTwo))
        {
            $dateTimeTwo = $dateTimeOne;
            $dateTimeOne = date("Y-m-d H:i:s");
        }
        
        $dateOne = substr($dateTimeOne , 0, 10);
        
        $dateOne = explode("-", $dateOne);                                 
        $timeOne = substr($dateTimeOne , 11);
        $timeOne = explode(":", $timeOne);
           
        $dateTwo = substr($dateTimeTwo , 0, 10);
        $dateTwo = explode("-", $dateTwo);                                 
        $timeTwo = substr($dateTimeTwo , 11);
        $timeTwo = explode(":", $timeTwo);                        
        
        switch ($type)
        {
            case 'Y': $converter = 31536000; break;
            case 'M': $converter = 2592000; break;
            case 'D': $converter = 86400; break;
            case 'H': $converter = 3600; break;
            case 'I': $converter = 60; break;                    
            case "S": $converter = 1; break;                                                                                
            default: $converter = 1;
        }
                                
        return round( 
                        ( 
                            (                                                                   
                                mktime($timeOne[0], $timeOne[1], $timeOne[2], $dateOne[1], $dateOne[2], $dateOne[0])
                                -
                                mktime($timeTwo[0], $timeTwo[1], $timeTwo[2], $dateTwo[1], $dateTwo[2], $dateTwo[0])
                            ) 
                            / $converter
                        )
                    );
    }
    
    
    /**
    * Returns the difference between a date and other
    * @access public
    * @return int
    * @param $dateOne datetime or date
    * @param $dateTwo datetime or date
    * @param $type char ['Y','M','D','H', 'I', 'S']
     *@param $tokenSlipt
    * @param $emailFacebook String Email using in the facebook
    */ 
    public static function convertSecondsTo($seconds, $inicio = 'Y') 
    {

        $dias_por_mes = ((((365*3)+366)/4)/12);

        $comecou = false;

        if ($inicio == 'Y')
        {
            $array['anos'] = floor( $seconds / (60*60*24* $dias_por_mes *12) );
            $seconds = ($seconds % (60*60*24* $dias_por_mes *12));
            $comecou = true;
        }
        if (($inicio == 'm') || ($comecou == true))
        {
            $array['meses'] = floor( $seconds / (60*60*24* $dias_por_mes ) );
            $seconds = ($seconds % (60*60*24* $dias_por_mes ));
            $comecou = true;
        }
        if (($inicio == 'd') || ($comecou == true))
        {
            $array['dias'] = floor( $seconds / (60*60*24) );
            $seconds = ($seconds % (60*60*24));
            $comecou = true;
        }
        if (($inicio == 'H') || ($comecou == true))
        {
            $array['horas'] = floor( $seconds / (60*60) );
            $seconds = ($seconds % (60*60));
            $comecou = true;
        }
        if (($inicio == 'i') || ($comecou == true))
        {
            $minutos = floor($seconds / 60);
            
            
            $array['minutos'] = $minutos;
            
            $seconds = ($seconds % 60);
            $comecou = true;
        }
                
        $array['segundos'] = $seconds;
                
        return $array;
    }
    
    public static function now()
    {
        return date("Y-m-d H:i:s");
    }
}

?>
